import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/entity/device_mine_entity.dart';
import 'package:oramemo/pages/facility/blue/blue_logic.dart';
import 'package:oramemo/pages/facility/facility_router.dart';
import 'package:oramemo/pages/first/first_router.dart';
import 'first_logic.dart';
import 'widgets/device_add_card.dart';
import 'widgets/device_card.dart';

class DeviceView extends StatelessWidget {
  const DeviceView({super.key, required this.devices});

  final List<DeviceMineData> devices;

  @override
  Widget build(BuildContext context) {
    Get.put(BlueLogic());
    final logic = Get.find<FirstLogic>();
    final itemCount = devices.length + 1;
    return Obx(() {
      int currentIndex = logic.currentIndex.value;
      return Column(
        children: [
          Container(
            height: 180.h,
            alignment: Alignment.center,
            child: PageView.builder(
              onPageChanged: (index) {
                logic.currentIndex.value = index;
              },
              itemCount: itemCount, // 示例：3个设备
              itemBuilder: (context, index) {
                if (index == devices.length) {
                  return const DeviceAddCard();
                } else {
                  return DeviceCard(
                    device: devices[index],
                    onTap: () {
                      // 处理卡片点击
                      // Get.toNamed(FirstRouter.deviceSetting,
                      //     arguments: devices[index]);

                      Get.toNamed(FacilityRouter.blueSettingPage,arguments: devices[index]);
                    },
                  );
                }
              },
            ),
          ),
          SizedBox(height: 20.h),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: itemCount > 1
                ? List.generate(
                    itemCount,
                    (index) {
                      if (index == currentIndex) {
                        return Container(
                          width: 16.w,
                          height: 6.w,
                          margin: EdgeInsets.symmetric(horizontal: 4.w),
                          decoration: ShapeDecoration(
                            color: const Color(0xFF2573FF),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(3)),
                          ),
                        );
                      } else {
                        return Container(
                          width: 6.w,
                          height: 6.w,
                          margin: EdgeInsets.symmetric(horizontal: 4.w),
                          decoration: ShapeDecoration(
                            color: const Color(0xFF92B9FF),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(3)),
                          ),
                        );
                      }
                    },
                  )
                : [Container()],
          ),
        ],
      );
    });
  }
}
